﻿using Chloe.Infrastructure;
using Chloe.MySql;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using Chloe.Exceptions;
using Chloe;

namespace EasyToHelp.DBFactory
{
    public class MySqlConnectionFactory : IDbConnectionFactory
    {
        #region 连接数据库基本配置
        string _connString = null;
        public MySqlConnectionFactory(string connString)
        {
            this._connString = connString;
        }
        public IDbConnection CreateConnection()
        {
            IDbConnection conn = new MySqlConnection(this._connString);
            conn = new Chloe.MySql.ChloeMySqlConnection(conn);
            return conn;
        }
        #endregion
    }

    public static class ContextHelper
    {
        #region 数据上下文
        public static string _connString = string.Empty;
        /// <summary>
        /// 数据上下文
        /// </summary>
        /// <param name="_connString"></param>
        /// <returns></returns>
        public static MySqlContext Context(string _connString)
        {
            MySqlContext context = new MySqlContext(new MySqlConnectionFactory(_connString));
            return context;
        }
        #endregion
    }
    public static class DBHelper
    {
        #region 新增
        public static T InsertExecute<T>(T model) where T : class
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Insert(model);
            }
        }
        #endregion

        #region 编辑
        public static int UpdateExecute<T>(T model) where T : class
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Update(model);
            }
        }
        #endregion

        #region 删除
        public static int DeleteExecute<T>(T model) where T : class
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Delete(model);
            }
        }
        #endregion

        #region 返回泛型
        public static List<T> GetList<T>() where T : class
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Query<T>().ToList();
            }
        }
        /// <summary>
        /// sql语句返回集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static IEnumerable<T> GetList<T>(string sql) where T : class
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.SqlQuery<T>(sql);
            }
        }

       
        #endregion

        #region 获取DataReader
        /// <summary>
        /// 获取DataReader
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static IDataReader GetDataReader(string SqlQuery)
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {

                return db.Session.ExecuteReader(SqlQuery);
            }
        }
        #endregion

        #region 获取DataTable
        /// <summary>
        /// 获取DataTable
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        //public static DataTable GetDataTable(string SqlQuery)
        //{
        //    using (var db = ContextHelper.Context(ContextHelper._connString))
        //    {
        //        return db.Session.ExecuteDataTable(SqlQuery);
        //    }

        //}
        #endregion

        #region 执行增删改
        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <returns></returns>
        public static int ExecuteNonQuery(string SqlQuery)
        {
            using (var db = ContextHelper.Context(ContextHelper._connString))
            {
                return db.Session.ExecuteNonQuery(SqlQuery);
            }
        }
        #endregion
    }
}
